/**
 * Created by Administrator on 2017/8/25.
 */
//  function initCurrentUser(){
//     $$ajax({
//         url: "/agent/current",
//         type: "get",
//         contentType: 'application/json',
//         success: function(response) {
// 		   alert("哈哈")
// 		   if (response.code == 200) {   
// 			alert("你好")		   
//                 $("#avatar").attr("src", response.data.avatar);
//                 $("#accountName").text(response.data.agentName);
//                 //$("#userName").html(response.data.userName+"<b class='caret'>");
// 				//$("#side-menu").append("<li>米振</li>")
// 			if(response.data.userType==1){
// 				$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的团队</a></li>");
// 				$.initMetsiMenu();
// 				//$("#side-menu").append("<li><a href='./html/app_agent/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>")
// 			}else if(response.data.userType==2){
// 					$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_expert/list.html?id="+response.data.id+"' data-index='0'>我的讲师团队</a></li>");
// 				//$("#side-menu").append("<li><a href='./html/app_expert/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的讲师团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>");				
// 				$.initMetsiMenu();
// 			}
			 
// 			} else if(response.code == 401){
//             	window.location.href = "login.html";							
//             }
			
// 			}
		
//             //tableInit.myViewModel.refresh();
//         });
//  }
var __logout = "../logout"; //post 方式 退出登录
var __getMenu = "/user/read/current/menu";
$(function() {
	 
    setTimeout(initCurrentUser(),400);
	function initCurrentUser(){
		var menuArr = [1];
		var arr = [0];

		$$ajax({
			url: "/agent/current",
			type: "get",
			contentType: 'application/json',
			timeout: 1000,    
	
			success: function(response) {
			   
			   if (response.code == 200) {   
					$("#accountName").text(response.data.agentName);
					//$("#userName").html(response.data.userName+"<b class='caret'>");
					//$("#side-menu").append("<li>米振</li>")
				if(response.data.userType==1){
					$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的团队</a></li><li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的收益列表</a></li>");
					initMenu(arr,menuArr);
					$.initMetsiMenu();
					//$("#side-menu").append("<li><a href='./html/app_agent/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>")
				}else if(response.data.userType==2){
						$("#side-menu").append("<li><a class='J_menuItem' href='./html/app_expert/list.html?id="+response.data.id+"' data-index='0'>我的讲师团队</a></li><li><a class='J_menuItem' href='./html/app_agent/list.html?id="+response.data.id+"' data-index='0'>我的收益列表</a></li>");
					//$("#side-menu").append("<li><a href='./html/app_expert/list.html?id="+response.data.id+"'><i class='fa fa-home'></i><span class='nav-label' >我的讲师团队</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' ></ul></li>");				
					initMenu(arr,menuArr);
					$.initMetsiMenu();
				}
				 
				} else if(response.code == 401){
					window.location.href = "login.html";							
				}
				
				},
				error: function (textStatus) {
					 
					alert(textStatus);
					window.location.href = "login.html";		
					console.error(textStatus);
				},
				complete: function (XMLHttpRequest, statusText) {
					if (statusText == 'timeout') {
						xhr.abort();  
						alert('Time out');
					}
				},
				//tableInit.myViewModel.refresh();
			});
	 }


})

function initMenu (fatherArray,sonArray) {
	var newArray = [];
	for(var i in fatherArray){
		for(var j = 0;j<sonArray.length;j++){
			if(sonArray[j].parentId==fatherArray[i].id){
				if(sonArray[j].leaf=="true"){
					var name = sonArray[j].permission.replace(/\./g,"_");
					$("#"+fatherArray[i].id).append("<li><a class='J_menuItem' href='html/"+name+"/list.html' data-index='0'>"+sonArray[j].menuName+"</a></li>");
					sonArray.splice(j,1);
					j--;
				}else{
					$("#"+fatherArray[i].id).append("<li><a href='#'><i class='fa fa-home'></i><span class='nav-label' >"+sonArray[j].menuName+"</span><span class='fa arrow'></span></a><ul class='nav nav-second-level' id='"+sonArray[j].id+"'></ul></li>");
					sonArray.splice(j,1);
					newArray.push(sonArray[j]);
					j--;
				}
			}
		}
	}
	if(newArray.length>0){
		initMenu(newArray,sonArray);
	}
}
$(".logout").click(function(){
    logout();
})
function  logout(){
    $$ajax({
        url: __logout,
        type: 'post',//提交的方式
        dataType:'json',
        async:true,
        contentType:"application/json; charset=utf-8",
        success: function(data) {
            if (data.httpCode==200) {
                console.log(data.msg)
                //alert(data.msg)
            } else{
                alert(data.msg);
            }
        }

    });
}
